define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'document/verify/index',
                    verify_url: 'document/verify/verify',
                    table: 'document',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'type',
                            title: __('Type'),
                            formatter: function(value) {
                                var statuses = [__('RentNew'), __('RentRenew'), __('RentSublet')];
                                return statuses[value]
                            }
                        },

                        { field: 'code', title: __('Code') },
                        { field: 'name', title: __('Name') },
                        {
                            field: 'company',
                            title: __('Company'),
                            formatter: function(company) {
                                if (company) {
                                    return company.companyname;
                                }
                                return null;
                            }
                        },
                        {
                            field: 'verify',
                            title: __('VerifyStatus'),
                            formatter: function(value) {
                                var statues = [__('VerifyWait'), __('VerifyPass'), __('VerifyRefuse')];
                                return Table.api.formatter.status(statues[value], statues)
                            }
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [{
                                name: 'verify',
                                title: '审核',
                                icon: 'fa fa-pencil',
                                classname: 'btn btn-verify btn-xs btn-success'
                            }]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            table.on('click', '.btn-verify', function(e) {
                e.stopPropagation();
                var options = table.bootstrapTable('getOptions');
                var index = $(this).closest('tr').data('index');
                var data = table.bootstrapTable('getData');
                var id = data[index][options.pk]

                Fast.api.open(options.extend.verify_url + (id > 0 ? (options.extend.verify_url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + id : ''), __('Verify'))
            })
        },
        verify: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
                $('input[name=\'row[type]\']').attr('disabled', true)
                $('.btn-trash').remove()
                $('#plupload-image').parent().remove()
                $('#fachoose-image').parent().remove()
            }
        }
    };
    return Controller;
});